﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class frmLoanDetail3 : Form
    {
        private static CustomerItem cusItem;
        //private static EmployeeItem empItem;
        private static BondsmanItem bondsmanItem;
        public frmLoanDetail3()
        {
            InitializeComponent();
        }

        public frmLoanDetail3(CustomerItem item)
        {
            InitializeComponent();
            cusItem = item;
            bondsmanItem = DAL.BondsmanInfoDAL.LoadBondsmanByCusID(cusItem.CUSTOMER_ID);
            this.txtAddress.Text = bondsmanItem.BONDSMAN_ADDRESS;
            this.txtAddressWork.Text = bondsmanItem.BONDSMAN_WORK_ADDRESS;
            this.txtAge.Text = bondsmanItem.BONDSMAN_AGE.ToString();
            this.txtJob.Text = bondsmanItem.BONDSMAN_JOB;
            this.txtLastName.Text = bondsmanItem.BONDSMAN_LASTNAME;
            this.txtName.Text = bondsmanItem.BONDSMAN_NAME;
            this.txtPresonalId.Text = bondsmanItem.BONDSMAN_PERSONAL_ID;
            this.txtTel.Text = bondsmanItem.BONDSMAN_TEL;
            this.txtTelWork.Text = bondsmanItem.BONDSMAN_WORK_TEL;
            this.txtSalary.Text = bondsmanItem.BONDSMAN_SARARY.ToString();
            
            genGridviewDoc();

            if (bondsmanItem.CHECK_IDENTITY_CARD == "A")
            {
                this.checkBox1.Checked = true;
            }
            if (bondsmanItem.CHECK_REGISTRATION == "A")
            {
                this.checkBox3.Checked = true;
            }
            if (bondsmanItem.CHECK_SALARY_SLIP == "A")
            {
                this.checkBox5.Checked = true;
            }
            if (bondsmanItem.CHECK_ACCOUNT == "A")
            {
                this.checkBox7.Checked = true;
            }
            this.grvDoc.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentDocClick);
        }

        private void CellContentDocClick(object sender, DataGridViewCellEventArgs e)
        {
            CustomerDocItem item = DAL.CustomerDocInfoDAL.LoadCusDoc(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()));
            var tempFolder = System.IO.Path.GetTempPath();
            string filename = System.IO.Path.Combine(tempFolder, item.CUSTOMER_DOC_NAME + item.CUSTOMER_DOC_EXTENSION);
            System.IO.File.WriteAllBytes(filename, item.CUSTOMER_DOC_FILE);
            System.Diagnostics.Process.Start(filename);
        }

        private void genGridviewDoc()
        {
            // gen Grideview
            grvDoc.AutoGenerateColumns = false;
            //Set Columns Count
            grvDoc.ColumnCount = 3;
            grvDoc.Columns[0].Width = 50;
            //Add Columns
            grvDoc.Columns[1].Name = "BONDSMAN_DOC_ID";
            grvDoc.Columns[1].HeaderText = "รหัสเอกสาร";
            grvDoc.Columns[1].DataPropertyName = "BONDSMAN_DOC_ID";
            grvDoc.Columns[1].Width = 70;

            grvDoc.Columns[2].Name = "BONDSMAN_DOC_NAME";
            grvDoc.Columns[2].HeaderText = "ชื่อเอกสาร";
            grvDoc.Columns[2].DataPropertyName = "BONDSMAN_DOC_NAME";
            grvDoc.Columns[2].Width = 350;

            this.grvDoc.DataSource = LoadDocCustomer();
        }

        private DataTable LoadDocCustomer()
        {
            DataTable dt = BondsmanDocInfoDAL.LoadBondsmanDocAll(bondsmanItem.BONDSMAN_ID);
            return dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //บัตรประจำตัวประชาชน
                if (checkBox1.Checked)
                {
                    bondsmanItem.CHECK_IDENTITY_CARD = "A";
                }
                else if (checkBox2.Checked)
                {
                    bondsmanItem.CHECK_IDENTITY_CARD = "F";
                }
                else
                {
                    bondsmanItem.CHECK_IDENTITY_CARD = "I";
                }

                //ทะเบียนบ้าน
                if (checkBox3.Checked)
                {
                    bondsmanItem.CHECK_REGISTRATION = "A";
                }
                else if (checkBox4.Checked)
                {
                    bondsmanItem.CHECK_REGISTRATION = "F";
                }
                else
                {
                    bondsmanItem.CHECK_REGISTRATION = "I";
                }

                //สลิปเงินเดือน หรือหนังสือรับรองเงินเดือน
                if (checkBox5.Checked)
                {
                    bondsmanItem.CHECK_SALARY_SLIP = "A";
                }
                else if (checkBox6.Checked)
                {
                    bondsmanItem.CHECK_SALARY_SLIP = "F";
                }
                else
                {
                    bondsmanItem.CHECK_SALARY_SLIP = "I";
                }

                //รายละเอียดการเดินบัญชีย้อนหลัง 6 เดือน
                if (checkBox7.Checked)
                {
                    bondsmanItem.CHECK_ACCOUNT = "A";
                }
                else if (checkBox8.Checked)
                {
                    bondsmanItem.CHECK_ACCOUNT = "F";
                }
                else
                {
                    bondsmanItem.CHECK_ACCOUNT = "I";
                }
                
                int salary = 0;
                bool result = Int32.TryParse(this.txtSalary.Text, out salary);
                if (result)
                {
                    bondsmanItem.BONDSMAN_SARARY = Convert.ToInt32(this.txtSalary.Text);
                }
                else
                {
                    bondsmanItem.BONDSMAN_SARARY = salary;
                }
                if (checkBox5.Checked && bondsmanItem.BONDSMAN_SARARY == 0)
                {
                    MessageBox.Show("ไม่สามารถเลือก สลิปเงินเดือน เป็นถูกต้องได้หากยังไม่ได้กรอกเงินเดือน", "บันทึกข้อมูลผู้ค้ำประกัน");
                    return;
                }
                BondsmanInfoDAL.UpdateSaraly(bondsmanItem);
                MessageBox.Show("บันทึกข้อมูลสำเร็จ", "บันทึกข้อมูลผู้ค้ำประกัน");
            }
            catch (Exception ex) 
            {
                MessageBox.Show("ex : " + ex.Message, "บันทึกผิดพลาด");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                checkBox2.Checked = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox3.Checked = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                checkBox4.Checked = false;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox3.Checked = false;
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                checkBox6.Checked = false;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                checkBox5.Checked = false;
            }
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                checkBox8.Checked = false;
            }
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                checkBox7.Checked = false;
            }
        }
    }
}
